const axios = require('../../utils/axios');
Page({
  data: {
    pages: 1,
    data: [
      { time: '4-8 23:12', is_top: true, n_title: '公告标题', n_content: '公告具体内容' }
    ]
  },
  onLoad(options) {
    // this.getData();
  },

  async getData(page = 1) {
    axios.get('/notice?size=10&page=' + page).then(res => {
      const data =res.items;
      data.forEach(item => {
        item.time = getTimeStr(item.create_time);
      })
      console.log(data);
      this.setData({
        data,
        pages: Math.ceil(res.counts / 10)
      })
    })
  },

  onChangePage(e) {
    this.getData(e.detail);
  },

  onShowDetail(e) {
    console.log(e);
  //   const { item } = e.currentTarget.dataset;
  //   nav.index.add_notice_view(item);
  //   if(item.c_id) {
  //     wx.navigateTo({
  //       url: '/pages/comp/comp?id=' + item.c_id,
  //     })
  //   } else {
  //     this.setData({
  //       detail: item ? item : this.data.detail,
  //       detail_show: !this.data.detail_show
  //     })
  //   }
  },
})

function getTimeStr(time) {
  const date = new Date(time || null);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes() + '';
  const now = new Date();
  const current_yaer = now.getFullYear();
  const current_month = now.getMonth() + 1;
  const current_day = now.getDate();
  return `${year == current_yaer ? '' : year + '-'}${month == current_month && day == current_day ? '' : `${month}-${day} `}${hour}:${minute.padStart(2, '0')}`;
}